package com.sky.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 获取开始时间与结束时间内的时间(包含两头)
 * @author mc
 * @date 2022/10/19
 */
public class TimeUtil {

    /**
     *  获取两个时间中间的日期(天)
     * @param startDate "2022-01-09"
     * @param endDate "2022-01-12"
     * @return [2022-01-09, 2022-01-10, 2022-01-11, 2022-01-12]
     */
    public static List<String> getBetweenDay(String startDate, String endDate) throws Exception {
        Date start = null;//定义起始日期
        Date end = null;//定义结束日期
        try {
            start = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
            end = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date date = start;
        Calendar cd = Calendar.getInstance();//用Calendar 进行日期比较判断
        while (date.getTime()<= end.getTime()){
            result.add(sdf.format(date));
            cd.setTime(date);
            cd.add(Calendar.DATE, 1);//增加一天 放入集合
            date=cd.getTime();
        }
        return result;
    }

    /**
     *  获取两个时间中间的日期(月)
     * @param startDate "2022-01"
     * @param endDate "2022-05"
     * @return [2022-01, 2022-02, 2022-03, 2022-04, 2022-05]
     */
    public static List<String> getBetweenMonth(String startDate, String endDate) throws Exception {
        Date start = null;//定义起始日期
        Date end = null;//定义结束日期
        try {
            start = new SimpleDateFormat("yyyy-MM").parse(startDate);
            end = new SimpleDateFormat("yyyy-MM").parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Date date = start;
        Calendar cd = Calendar.getInstance();//用Calendar 进行日期比较判断
        while (date.getTime()<= end.getTime()){
            result.add(sdf.format(date));
            cd.setTime(date);
            cd.add(Calendar.MONTH, 1);//增加一天 放入集合
            date=cd.getTime();
        }
        return result;
    }

    /**
     *  获取两个时间中间的日期(年)
     * @param startDate "2022"
     * @param endDate "2025"
     * @return [2022, 2023, 2024, 2025]
     */
    public static List<String> getBetweenYear(String startDate, String endDate) throws Exception {
        Date start = null;//定义起始日期
        Date end = null;//定义结束日期
        try {
            start = new SimpleDateFormat("yyyy").parse(startDate);
            end = new SimpleDateFormat("yyyy").parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

        Date date = start;
        Calendar cd = Calendar.getInstance();//用Calendar 进行日期比较判断
        while (date.getTime()<= end.getTime()){
            result.add(sdf.format(date));
            cd.setTime(date);
            cd.add(Calendar.YEAR, 1);//增加一天 放入集合
            date=cd.getTime();
        }
        return result;
    }
}
